package com.songcan.basics.core.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.songcan.basics.core.service.AreaService;
import com.songcan.common.exception.ParamNotException;
import com.songcan.model.basics.Area;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/rest/area")
@AllArgsConstructor
public class AreaController {



    private AreaService areaService;


    @PostMapping()
    public void addArea(@NotEmpty(message = "名称不能为空") String name,
                        String shortName,String remark,String mergerName,
                        @NotNull(message = "图片不能为空") String iconPath,
                        String pinyin,Long parentId,String initial,
                        @NotNull(message = "纬度不能为空") BigDecimal lng,
                        @NotNull(message = "经度不能为空")BigDecimal lat){
        areaService.addArea(name,parentId,remark,iconPath,shortName,mergerName,initial,pinyin,lng,lat);
    }

    @PutMapping
    public void updateArea(@RequestBody Area area){
        if(Objects.isNull(area.getId())){
            throw new ParamNotException("id");
        }
        areaService.saveOrUpdate(area);
    }

    @DeleteMapping()
    public void deleteArea(@NotNull(message = "ids不能为空")String... ids){
        areaService.removeByIds(Arrays.asList(ids).parallelStream().map(Long::valueOf).collect(Collectors.toList()));
    }

    @GetMapping("/list")
    public IPage<Area> list(@RequestParam(defaultValue = "1")Integer page,
                            @RequestParam(defaultValue = "20")Integer pageSize){
        return  areaService.page(new Page<>(page,pageSize));
    }

    @GetMapping()
    public Area getById(@NotNull(message = "id不能为空") String id){
        return areaService.getById(id);
    }

}
